package club.tclyq.good_time.common.json.serialiazer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import org.bson.types.ObjectId;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;

/**
 * @author tanchong
 * Create Date: 2019/2/27
 */
@JsonComponent
public class ObjectIdCombinedSerializer {
    public static class ObjectIdJsonSerializer extends JsonSerializer<ObjectId> {
        @Override
        public void serialize(ObjectId objectId, JsonGenerator jsonGenerator,
                              SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(objectId.toString());

        }
    }
    public static class ObjectIdJsonDeserializer extends JsonDeserializer<ObjectId> {
        @Override
        public ObjectId deserialize(JsonParser parser, DeserializationContext deserializationContext)
                throws IOException {
            var node = (JsonNode)parser.getCodec().readTree(parser);
            return new ObjectId(node.asText());
        }
    }
}
